<?php
include_once 'site_config.php';
include_once "ra_global.php";

if(!$_GET['code']){
    echo "<script language=javascript>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".urlencode(SiteUrl.'oildiscount.php')."&response_type=code&scope=snsapi_base&state=1#wechat_redirect';</script>";
}

//获取code
$getcode=getToken("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$_GET['code']."&grant_type=authorization_code");
$user_obj=json_decode($getcode);

if($user_obj->openid){
    $user=$db->ig2_want('mae_user','openid="'.trim($user_obj->openid).'" and is_delete=0');
    if(!$user){
        echo '<script>location.href="user.php";</script>';
    }
}else{
    echo "<script language=javascript>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".urlencode(SiteUrl.'oildiscount.php')."&response_type=code&scope=snsapi_base&state=1#wechat_redirect';</script>";
}

function getToken($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $row=curl_exec($ch);
    return $row;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width" />
<meta content="telephone=no" name="format-detection" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>优惠码验证</title>
<link href="css/discount.css" rel="stylesheet" type="text/css" />
<link href="css/common.css" rel="stylesheet" type="text/css" />
<style type = "text/css">
	*::-webkit-input-placeholder{
		color:#FFFFFF;
	}
</style>
<script src="inc/jquery.min.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
$(document).ready(function() {
    $("#validate").bind("click", function(){
        $(this).css({'background':'#F8D62E'});
        var userid = $("#userid").val();
        var promocode = $("#promocode").val();

        if (promocode == null || promocode == "") {
            alert("请输入优惠码!");
            $(this).css({'background': '#F09802'});
            return;
        }

        $.ajax({
            type: "GET",
            url: "ra_ajax_oildiscount.php",
            dataType: 'json',
            data: {
                method: "checkcode",
                promocode: promocode,
                userid: userid
            },
            success: function(data){
                // 如果验证失败
                if (data.errcode == 1) {
                    window.location = 'oildiscount_error.php?errmsg=' + data.errmsg;
                }
                // 如果验证成功
                else if (data.errcode == 0) {
                    window.location = 'oildiscount_success.php';
                }
                $(this).css({'background':'#F09802'});
            }
        });
    });
});
</script>
<body>
<div id="discount_body">

<div class="discount_checkcon">
<div class="discount_checkcon_title">优惠码验证</div>
<div class="discount_checkcon_input">
<div class="discount_checkcon_input_inner">
<div class="discount_checkcon_input_inner_left">优惠码</div>
<div class="discount_checkcon_input_inner_right">
<input id="userid" name="userid" type="hidden" value="<?php echo($user['id'])?>" />
<input id="promocode" name="promocode" type="text" placeholder="请输入顾客的优惠码" style="color:#fff;">
</div>
</div>

</div>
<div class="discount_checkcon_btn"><a href="javascript:void(0);" id="validate">提交验证</a></div>
<div class="foot_txt" style="color:#7f7c7c;font-size:12px;width:100%;text-align:center;position:fixed;bottom:5px;">本活动最终解释权归壳牌所有</div>
</div>
</div>
</body>
</html>
